using System;
using System.Drawing;
using System.Windows.Forms;
using System.Collections;
using System.Collections.Generic;

using SBPweb.Modules.Smartparts;


namespace SBPweb.Presentation.Windows.Forms.Controls
{
    public enum ToolStripSizes
    {
        Small,
        Medium,
        Large
    }

    public class MenuToolStrip : ToolStrip, IMenuList
    {
        public MenuToolStrip() : base()
        {
            base.ItemAdded += new ToolStripItemEventHandler(MenuToolStrip_ItemAdded);
            base.ItemRemoved += new ToolStripItemEventHandler(MenuToolStrip_ItemRemoved);
            base.GripStyle = ToolStripGripStyle.Hidden;
        }

        #region IMenuList Members

        public IMenuItem[] MenuItems
        {
            get
            {
                IMenuItem[] ret = (IMenuItem[])Array.CreateInstance(typeof(IMenuItem), base.Items.Count);

                int i = 0;
                foreach (ToolStripItem tsi in base.Items)
                {
                    ret[i++] = (IMenuItem)tsi;
                }

                return ret;
            }
        }

        public new event EventHandler<MenuEventArgs> ItemAdded;

        public new event EventHandler<MenuEventArgs> ItemRemoved;

        #endregion

        #region IList<IMenuItem> Members

        public int IndexOf(IMenuItem item)
        {
            return base.Items.IndexOf((ToolStripItem)item);
        }

        public void Insert(int index, IMenuItem item)
        {
            base.Items.Insert(index, (ToolStripItem)item);
        }

        public void RemoveAt(int index)
        {
            base.Items.RemoveAt(index);
        }

        public IMenuItem this[int index]
        {
            get
            {
                return (IMenuItem)base.Items[index];
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        #endregion

        #region ICollection<IMenuItem> Members

        public void Add(IMenuItem item)
        {
            base.Items.Add((ToolStripItem)item);
        }

        public void Clear()
        {
            base.Items.Clear();
        }

        public bool Contains(IMenuItem item)
        {
            return base.Items.Contains((ToolStripItem)item);
        }

        public void CopyTo(IMenuItem[] array, int arrayIndex)
        {
            throw new NotImplementedException();
        }

        public int Count
        {
            get
            {
                return base.Items.Count;
            }
        }

        public bool IsReadOnly
        {
            get
            {
                return base.Items.IsReadOnly;
            }
        }

        public bool Remove(IMenuItem item)
        {
            try
            {
                base.Items.Remove((ToolStripItem)item);
            }
            catch
            {
                return false;
            }
            return true;
        }

        #endregion

        #region IEnumerable<IMenuItem> Members

        public IEnumerator<IMenuItem> GetEnumerator()
        {
            throw new NotImplementedException();
        }

        #endregion

        #region IEnumerable Members

        IEnumerator IEnumerable.GetEnumerator()
        {
            return base.Items.GetEnumerator();
        }

        #endregion

        public void ResizeStrip(ToolStripSizes size)
        {
            ToolStrip strip = this;

            strip.SuspendLayout();
            bool originalStretch = strip.Stretch;
            strip.AutoSize = false;
            strip.Stretch = false;
            int mSize = GetSizeByToolStripSize(size);
            strip.ImageScalingSize = new Size(mSize, mSize);
            switch (strip.Orientation)
            {
                default:
                case Orientation.Horizontal:
                    strip.Size = new System.Drawing.Size(strip.Width, mSize + 3);
                    break;
                case Orientation.Vertical:
                    strip.Size = new System.Drawing.Size(mSize, strip.Height + 3);
                    break;
            }
            strip.Stretch = originalStretch;
            strip.ResumeLayout(true);

            strip.AutoSize = true;
        }

        void MenuToolStrip_ItemRemoved(object sender, ToolStripItemEventArgs e)
        {
            if (this.ItemRemoved != null)
            {
                this.ItemRemoved(this, new MenuEventArgs(e.Item as IMenuItem));
            }
        }

        void MenuToolStrip_ItemAdded(object sender, ToolStripItemEventArgs e)
        {
            if (this.ItemAdded != null)
            {
                this.ItemAdded(this, new MenuEventArgs(e.Item as IMenuItem));
            }
        }

        #region static methods

        public static Image GetSizedImageFromImage(Image image, ToolStripSizes tss)
        {
            int size = GetSizeByToolStripSize(tss);
            return image.GetThumbnailImage(size, size, null, IntPtr.Zero);
        }

        public static Image GetSizedImageFromIcon(Icon icon, ToolStripSizes tss)
        {
            int size = GetSizeByToolStripSize(tss);
            return icon.ToBitmap().GetThumbnailImage(size, size, null, IntPtr.Zero);
        }

        public static int GetSizeByToolStripSize(ToolStripSizes tss)
        {
            switch (tss)
            {
                case ToolStripSizes.Large:
                    return 32;
                case ToolStripSizes.Medium:
                    return 24;
                default:
                    return 16;
            }
        }

        #endregion

    }
}
